package com.ext.aic.utils;

import android.app.Activity;
import android.content.Context;

import androidx.fragment.app.Fragment;

import com.eyinfo.android_pure_utils.events.Action1;
import com.eyinfo.android_pure_utils.utils.ModelConvert;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;
import com.luck.picture.lib.basic.PictureSelectionPreviewModel;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

public class PictureUtils {
    private static volatile PictureUtils instance;

    public static PictureUtils getInstance() {
        if (instance == null) {
            synchronized (PictureUtils.class) {
                if (instance == null) {
                    instance = new PictureUtils();
                }
            }
        }
        return instance;
    }

    private static class PictureSelectResult implements OnResultCallbackListener<LocalMedia> {

        private Action1<ArrayList<LocalMedia>> call;

        public PictureSelectResult(Action1<ArrayList<LocalMedia>> call) {
            this.call = call;
        }

        @Override
        public void onResult(ArrayList<LocalMedia> arrayList) {
            if (call != null) {
                call.call(arrayList);
            }
        }

        @Override
        public void onCancel() {

        }
    }

    /**
     * 图片选择器
     *
     * @param context 上下文
     * @param maxNum  最大选择图片数量
     * @param call    图片选择后回调
     */
    public void showPicture(Context context, int maxNum, Action1<ArrayList<LocalMedia>> call) {
        PictureSelector.create(context)
                .openGallery(SelectMimeType.ofImage())
                .setImageEngine(GlideEngine.createGlideEngine())
                .setMaxSelectNum(maxNum)
                .forResult(new PictureSelectResult(call));
    }

    private PictureSelectionPreviewModel buildPreviewModel(Context context) {
        return PictureSelector.create(context)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                    @Override
                    public void onPreviewDelete(int position) {

                    }

                    @Override
                    public boolean onLongPressDownload(Context context, LocalMedia media) {
                        return false;
                    }
                });
    }

    /**
     * 图片预览
     *
     * @param activity        上下文
     * @param currentPosition 当前位置
     * @param items           图片列表
     */
    public <T extends LocalMedia> void preview(Activity activity, int currentPosition, List<T> items) {
        ArrayList<LocalMedia> previewList = toPreviewList(items);
        PictureSelectionPreviewModel previewModel = buildPreviewModel(activity);
        previewModel.startActivityPreview(currentPosition, false, previewList);
    }

    private <T extends LocalMedia> ArrayList<LocalMedia> toPreviewList(List<T> items) {
        ArrayList<LocalMedia> list = new ArrayList<>();
        if (!ObjectJudge.isNullOrEmpty(items)) {
            for (T item : items) {
                list.add(ModelConvert.toModel(item, LocalMedia.class));
            }
        }
        return list;
    }

    /**
     * 图片预览
     *
     * @param fragment        Fragment
     * @param currentPosition 当前位置
     * @param items           图片列表
     */
    public <T extends LocalMedia> void preview(Fragment fragment, int currentPosition, List<T> items) {
        ArrayList<LocalMedia> previewList = toPreviewList(items);
        PictureSelectionPreviewModel previewModel = buildPreviewModel(fragment.getContext());
        previewModel.startFragmentPreview(currentPosition, false, previewList);
    }
}
